﻿using MslDiary.Model;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace MslDiary.BLL
{
    public class A_AnnexBLL : MSL.Tool.Data.DBAccessBase<Model.A_Annex>
    {
        #region 构造

        public A_AnnexBLL()
            : base("A_Annex", "Id", true)
        {

        }
        #endregion

        /// <summary>
        /// 删除没有保存的 内容图片或附件
        /// </summary>
        public void DeleteTmpContentImage()
        {
            MSL.Tool.Data.DbHelper.Instance.ExecSql("DELETE FROM A_Annex WHERE FromTable='A_Diary' AND fromrecordid=0", new Hashtable());
        }
        /// <summary>
        /// 读取文件并缓存到缓存目录里
        /// </summary>
        /// <param name="recordId">主表Id</param>
        public bool ReadFileAddCachedByRecordId(int recordId)
        {
            List<A_Annex> list = base.GetList(" FromRecordId=" + recordId, "Id,Name,Suffix,Content", "CreateDate DESC ");
            if (list == null) return true;
            string savePath = MslDiary.Framework.Gloable.CachePath + recordId + "";
            MSL.Tool.IOHelper.Instance.CreateFolder(savePath);
            string path = String.Empty;
            foreach (A_Annex mod in list)
            {
                try
                {
                    path = savePath + "\\" + mod.Id + (mod.Suffix.StartsWith(".") ? mod.Suffix : "." + mod.Suffix);
                    if (File.Exists(path)) continue;

                    FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write);
                    fs.Write(mod.Content, 0, mod.Content.Length);
                    fs.Close();
                }
                catch (Exception ex)
                {
                    MslDiary.Framework.IO.LogHelper.SaveLog("BLL A_AnnexBLL ReadFileAddCachedByRecordId recordId[" + recordId + "]", ex.ToString());
                }
            }
            return true;
        }
        /// <summary>
        /// 保存附件
        /// </summary>
        /// <param name="id"></param>
        /// <param name="fullName">路径+名称 没有后缀</param>
        public bool SaveAttachById(int id, string fullName)
        {
            if (id == 0) return false;
            A_Annex mod = base.GetEntity(" Id=" + id);
            if (mod != null && mod.Content != null)
            {
                try
                {
                    string path = fullName + (mod.Suffix.StartsWith(".") ? mod.Suffix : "." + mod.Suffix);

                    FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write);
                    fs.Write(mod.Content, 0, mod.Content.Length);
                    fs.Close();
                    return true;
                }
                catch (Exception ex)
                {
                    MslDiary.Framework.IO.LogHelper.SaveLog("BLL A_AnnexBLL SaveAttachById id[" + id + "]", ex.ToString());
                }
            }
            return false;
        }

        /// <summary>
        /// 日记附件数量
        /// </summary>
        /// <param name="recordId"></param>
        public int DiaryAttachCountByRecordId(int recordId)
        {
            if (recordId == 0) return 0;
            object obj = MSL.Tool.Data.DbHelper.Instance.ExecScalar("SELECT COUNT(1) FROM A_Annex WHERE FromTable='A_Diary' AND FromField='' AND FromRecordId=" + recordId + " ", new System.Collections.Hashtable());
            if (obj == null) return 0;
            return Int32.Parse(obj.ToString());
        }
        public List<A_Annex> GetDiaryAttachList(int fromRecordId)
        {
            return base.GetList(
                " FromTable='A_Diary' AND FromField='' AND FromRecordId IN(0," + fromRecordId + " )  ",
                "Id,ShowName,Suffix,FileSize",
                "CreateDate DESC ");
        }
        /// <summary>
        /// 删除没有保存的日记附件 即 FromRecordId=0
        /// </summary>
        public void DeleteNoSaveDiaryAttach()
        {
            base.Delete(" FromTable='A_Diary' AND FromField='' AND FromRecordId=0 ");
        }
        /// <summary>
        /// 保存 没有保存的日记附件
        /// </summary>
        public void SaveNoSaveDiaryAttach(int fromRecordId)
        {
            if (fromRecordId == 0) return;
            MSL.Tool.Data.DbHelper.Instance.ExecSql(" UPDATE A_Annex SET FromRecordId=" + fromRecordId + " WHERE FromTable='A_Diary' AND FromField='' AND FromRecordId=0", new System.Collections.Hashtable());
        }
    }
}
